/*-------------------------
Client Includes
-------------------------*/
AddCSLuaFile( "cl_init.lua" )

AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "sh_shop.lua" )
AddCSLuaFile( "sh_player.lua" )

/*-------------------------
Server Includes
-------------------------*/
include( "sv_servertoclient.lua" )
include( "sv_score.lua" )
include( "sv_cashsystem.lua" )

include( "shared.lua" )
include( "sh_player.lua" )
include( "sh_shop.lua" )

/*-------------------------
Little Things
-------------------------*/
// If you are doing a custom version of this PLEASE change this thank you <3
//Do something like SVN revision# - MyVersion 5 
function ShowVersion( ply, cmd, args )
	local Version = " [Alpha] SVN r61"
	
	umsg.Start( "GPush_Notify", ply )
		umsg.String(Version)
		umsg.Long(5)
	umsg.End()
end
concommand.Add("p_version", ShowVersion)

/*-------------------------
Money
-------------------------*/
MONEY_STARTAMOUNT = 0 --probably best not to start with cash, dunno yet

function FirstSpawn( ply )
	ply:SetPData("Money", MONEY_STARTAMOUNT)
	ply:AddMoney(MONEY_STARTAMOUNT)
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )

/*-------------------------
Fretta
-------------------------*/
function GM:FragLimitThink()
	if ( GAMEMODE.IsEndOfGame ) then return end
	for k, ply in pairs( player.GetAll() ) do
		if ( !IsValid( ply ) ) then continue end
		if ( ply:Team() == TEAM_SPECTATOR ) then continue end
		if ( ply:Team() == TEAM_UNASSIGNED ) then continue end
	end
end

timer.Create( "FragLimitThink", 1, 0, function() GAMEMODE:FragLimitThink() end )

/*-------------------------
Manage Classes
-------------------------*/
function ClassSwap( ply, cmd, args ) --Function to swap classes
	if (args[1]=="gunner") then
		if(ply:Team() == 1) then
			ply:ChatPrint( "You will spawn as a Gunner")
			ply:SetPlayerClass("Fed_Gunner")
			ply:Kill()
			
		elseif (ply:Team() == 2) then
			ply:ChatPrint( "You will spawn as a Gunner")
			ply:SetPlayerClass("Raider_Gunner")
			ply:Kill()
		end
	end
	
	if (args[1]=="shotgunner") then
		if(ply:Team() == 1) then
			ply:ChatPrint( "You will spawn as a Shotgunner")
			ply:SetPlayerClass("Fed_Shotgunner")
			ply:Kill()
			
		elseif (ply:Team() == 2) then
			ply:ChatPrint( "You will spawn as a Shotgunner")
			ply:SetPlayerClass("Raider_Shotgunner")
			ply:Kill()
		end
	end
end
 
concommand.Add("p_classswap", ClassSwap)

/*-------------------------
Team Swapping
-------------------------*/
function GM:OnPlayerChangedTeam( ply, oldteam, newteam )
	//We cant let them keep their money
	ply:SetMoney(0)
	
	if ( newteam == TEAM_SPECTATOR ) then
		// If we changed to spectator mode, respawn where we are
		local Pos = ply:EyePos()
		ply:Spawn()
		ply:SetPos( Pos )
	elseif ( oldteam == TEAM_SPECTATOR ) then
		// If we're changing from spectator, join the game
		if ( !GAMEMODE.NoAutomaticSpawning ) then
				ply:Spawn()
		end
	else
	end
	// Send umsg for team change
	local rf = RecipientFilter();
	rf:AddAllPlayers();

	umsg.Start( "fretta_teamchange", rf );
				umsg.Entity( ply );
				umsg.Short( oldteam );
				umsg.Short( newteam );
	umsg.End();   
end

/*-------------------------
Death
-------------------------*/
function GM:DoPlayerDeath( ply, attacker, dmginfo )
 
	ply:CreateRagdoll()
	ply:AddDeaths(1)
	
	if(ply:GetLoot() >10)then
		CreateLootDrop(ply:GetPos(), ply:GetLoot())
		ply:SetLoot(0)
	end
	
	if ( attacker:IsValid() && attacker:IsPlayer() ) then
 
		if ( attacker == ply ) then
			attacker:AddFrags( -1 )
		else
			attacker:AddFrags( 1 )
		end
	end
end
/*-------------------------
Vital Hooks
-------------------------*/
function PrintUseInformation( ply, entity )
	--disables spectators from using "use"
	if(ply:Team() == 1002)then
		return false
	end
end
 
hook.Add( "PlayerUse", "PrintUseInformation", PrintUseInformation )